if j==1;
ix=0.1;
else
    ix=ixx(j-1);
end


x=(1-sqrt(1-2*etaX*(deltaX+ix-etaI*ix^2/2-deltaI)))/etaX*s;
errorix=rho/(1-1/psi)*((A-ix-x)/(rho*(1/(1-etaI*ix)))-1)+(ix-etaI/2*ix^2-deltaI)-gamma*sigma^2/2+lambdas/(1-gamma)*(betas/(betas+1-gamma)-1)+zetan(j)/(1-gamma)*((bsB(j)/((A-ix-x)^(1/(1-psi))*(rho*(1/(1-etaI*ix)))^(-psi/(1-psi))))^(1-gamma)-1);
while errorix>0
ix=ix+0.001;
x=(1-sqrt(1-2*etaX*(deltaX+ix-etaI*ix^2/2-deltaI)))/etaX*s;
errorix=rho/(1-1/psi)*((A-ix-x)/(rho*(1/(1-etaI*ix)))-1)+(ix-etaI/2*ix^2-deltaI)-gamma*sigma^2/2+lambdas/(1-gamma)*(betas/(betas+1-gamma)-1)+zetan(j)/(1-gamma)*((bsB(j)/((A-ix-x)^(1/(1-psi))*(rho*(1/(1-etaI*ix)))^(-psi/(1-psi))))^(1-gamma)-1);
end

while errorix<0
ix=ix-0.001;
x=(1-sqrt(1-2*etaX*(deltaX+ix-etaI*ix^2/2-deltaI)))/etaX*s;
errorix=rho/(1-1/psi)*((A-ix-x)/(rho*(1/(1-etaI*ix)))-1)+(ix-etaI/2*ix^2-deltaI)-gamma*sigma^2/2+lambdas/(1-gamma)*(betas/(betas+1-gamma)-1)+zetan(j)/(1-gamma)*((bsB(j)/((A-ix-x)^(1/(1-psi))*(rho*(1/(1-etaI*ix)))^(-psi/(1-psi))))^(1-gamma)-1);
end


while errorix>0
ix=ix*(1+1/10000);
x=(1-sqrt(1-2*etaX*(deltaX+ix-etaI*ix^2/2-deltaI)))/etaX*s;
errorix=rho/(1-1/psi)*((A-ix-x)/(rho*(1/(1-etaI*ix)))-1)+(ix-etaI/2*ix^2-deltaI)-gamma*sigma^2/2+lambdas/(1-gamma)*(betas/(betas+1-gamma)-1)+zetan(j)/(1-gamma)*((bsB(j)/((A-ix-x)^(1/(1-psi))*(rho*(1/(1-etaI*ix)))^(-psi/(1-psi))))^(1-gamma)-1);
end

while errorix<0
ix=ix*(1-1/200000);
x=(1-sqrt(1-2*etaX*(deltaX+ix-etaI*ix^2/2-deltaI)))/etaX*s;
errorix=rho/(1-1/psi)*((A-ix-x)/(rho*(1/(1-etaI*ix)))-1)+(ix-etaI/2*ix^2-deltaI)-gamma*sigma^2/2+lambdas/(1-gamma)*(betas/(betas+1-gamma)-1)+zetan(j)/(1-gamma)*((bsB(j)/((A-ix-x)^(1/(1-psi))*(rho*(1/(1-etaI*ix)))^(-psi/(1-psi))))^(1-gamma)-1);
end




while errorix>0
ix=ix*(1+1/100000000);
x=(1-sqrt(1-2*etaX*(deltaX+ix-etaI*ix^2/2-deltaI)))/etaX*s;
errorix=rho/(1-1/psi)*((A-ix-x)/(rho*(1/(1-etaI*ix)))-1)+(ix-etaI/2*ix^2-deltaI)-gamma*sigma^2/2+lambdas/(1-gamma)*(betas/(betas+1-gamma)-1)+zetan(j)/(1-gamma)*((bsB(j)/((A-ix-x)^(1/(1-psi))*(rho*(1/(1-etaI*ix)))^(-psi/(1-psi))))^(1-gamma)-1);
end

while errorix<0
ix=ix*(1-1/200000000);
x=(1-sqrt(1-2*etaX*(deltaX+ix-etaI*ix^2/2-deltaI)))/etaX*s;
errorix=rho/(1-1/psi)*((A-ix-x)/(rho*(1/(1-etaI*ix)))-1)+(ix-etaI/2*ix^2-deltaI)-gamma*sigma^2/2+lambdas/(1-gamma)*(betas/(betas+1-gamma)-1)+zetan(j)/(1-gamma)*((bsB(j)/((A-ix-x)^(1/(1-psi))*(rho*(1/(1-etaI*ix)))^(-psi/(1-psi))))^(1-gamma)-1);
end


while errorix>0
ix=ix*(1+1/10000000000);
x=(1-sqrt(1-2*etaX*(deltaX+ix-etaI*ix^2/2-deltaI)))/etaX*s;
errorix=rho/(1-1/psi)*((A-ix-x)/(rho*(1/(1-etaI*ix)))-1)+(ix-etaI/2*ix^2-deltaI)-gamma*sigma^2/2+lambdas/(1-gamma)*(betas/(betas+1-gamma)-1)+zetan(j)/(1-gamma)*((bsB(j)/((A-ix-x)^(1/(1-psi))*(rho*(1/(1-etaI*ix)))^(-psi/(1-psi))))^(1-gamma)-1);
end

while errorix<0
ix=ix*(1-1/20000000000);
x=(1-sqrt(1-2*etaX*(deltaX+ix-etaI*ix^2/2-deltaI)))/etaX*s;
errorix=rho/(1-1/psi)*((A-ix-x)/(rho*(1/(1-etaI*ix)))-1)+(ix-etaI/2*ix^2-deltaI)-gamma*sigma^2/2+lambdas/(1-gamma)*(betas/(betas+1-gamma)-1)+zetan(j)/(1-gamma)*((bsB(j)/((A-ix-x)^(1/(1-psi))*(rho*(1/(1-etaI*ix)))^(-psi/(1-psi))))^(1-gamma)-1);
end


 errorix
 
